package com.fxf.srb.core.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fxf.srb.core.enums.BorrowerStatusEnum;
import com.fxf.srb.core.enums.IntegralEnum;
import com.fxf.srb.core.mapper.BorrowerAttachMapper;
import com.fxf.srb.core.mapper.BorrowerMapper;
import com.fxf.srb.core.mapper.UserInfoMapper;
import com.fxf.srb.core.mapper.UserIntegralMapper;
import com.fxf.srb.core.pojo.entity.Borrower;
import com.fxf.srb.core.pojo.entity.BorrowerAttach;
import com.fxf.srb.core.pojo.entity.UserInfo;
import com.fxf.srb.core.pojo.entity.UserIntegral;
import com.fxf.srb.core.pojo.vo.BorrowerApprovalVO;
import com.fxf.srb.core.pojo.vo.BorrowerAttachVO;
import com.fxf.srb.core.pojo.vo.BorrowerDetailVO;
import com.fxf.srb.core.pojo.vo.BorrowerVO;
import com.fxf.srb.core.service.IBorrowerAttachService;
import com.fxf.srb.core.service.IBorrowerService;
import com.fxf.srb.core.service.IDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 借款人 服务实现类
 * </p>
 *
 * @author fxf
 * @since 2021-05-26
 */
@Service
public class BorrowerServiceImpl extends ServiceImpl<BorrowerMapper, Borrower> implements IBorrowerService {


    @Resource
    private BorrowerAttachMapper borrowerAttachMapper;
    @Resource
    private IDictService iDictService;
    @Resource
    private IBorrowerAttachService iBorrowerAttachService;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserIntegralMapper userIntegralMapper;


    /**
     * 保存借款人信息
     * @param borrowerVO
     * @param userId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBorrowerVOByUserId(BorrowerVO borrowerVO, Long userId) {

        Borrower borrower = baseMapper.selectById(userId);
        //保存借款人信息
        borrower.setAge(borrowerVO.getAge());
        borrower.setSex(borrower.getAge());
        borrower.setMarry(borrower.getMarry());
        borrower.setEducation(borrower.getEducation());
        borrower.setIndustry(borrower.getIndustry());
        borrower.setIncome(borrower.getIncome());
        borrower.setContactsMobile(borrower.getContactsMobile());
        borrower.setContactsName(borrower.getContactsName());
        borrower.setContactsRelation(borrower.getContactsRelation());

        List<BorrowerAttach> borrowerAttachList = borrowerVO.getBorrowerAttachList();
//        for (BorrowerAttach borrowerAttach: borrowerAttachList) {
//            borrowerAttach.setBorrowerId(borrower.getId());
//            iBorrowerAttachService.save(borrowerAttach);
//        }
        borrowerAttachList.forEach(borrowerAttach -> {
            borrowerAttach.setBorrowerId(borrower.getId());
            borrowerAttachMapper.insert(borrowerAttach);
        });

    }

    @Override
    public Integer getBorrowerStatus(Long userId) {
        QueryWrapper<Borrower> wrapper = new QueryWrapper<>();
        wrapper.select("status").eq("user_id",userId);
        List<Object> objects = baseMapper.selectObjs(wrapper);

        if (objects.size() == 0){
            return BorrowerStatusEnum.NO_BRROWER.getStatus();
        }
        Integer status = (Integer)objects.get(0);
        return status;


    }

    @Override
    public IPage<Borrower> listPage(Page<Borrower> pageParam,String keyword) {
        QueryWrapper<Borrower> wrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(keyword)){
            baseMapper.selectPage(pageParam,null);
        }

        wrapper.like("name",keyword)
                .or().like("is_card",keyword)
                .or().like("mobile",keyword)
                .orderByDesc("id");
        Page<Borrower> borrowerPage = baseMapper.selectPage(pageParam, wrapper);


        return borrowerPage;
    }

    @Override
    public BorrowerDetailVO getBorrowerDetailsVoById(Long id) {

        Borrower borrower = baseMapper.selectById(id);
        BorrowerDetailVO borrowerDetailVO = new BorrowerDetailVO();
        //借款人基本信息
        BeanUtils.copyProperties(borrower,borrowerDetailVO);

        borrowerDetailVO.setMarry(borrower.getMarry()?"是":"否");
        borrowerDetailVO.setSex(borrower.getSex()==1?"男":"女");

        //计算下拉列表选中内容
        String education = iDictService.getNameByParentDictCodeAndValue("education", borrower.getEducation());
        String industry = iDictService.getNameByParentDictCodeAndValue("moneyUse", borrower.getIndustry());
        String income = iDictService.getNameByParentDictCodeAndValue("income", borrower.getIncome());
        String returnSource = iDictService.getNameByParentDictCodeAndValue("returnSource", borrower.getReturnSource());
        String contactsRelation = iDictService.getNameByParentDictCodeAndValue("relation", borrower.getContactsRelation());

        borrowerDetailVO.setEducation(education);
        borrowerDetailVO.setIndustry(industry);
        borrowerDetailVO.setIncome(income);
        borrowerDetailVO.setReturnSource(returnSource);
        borrowerDetailVO.setContactsRelation(contactsRelation);

        String status = BorrowerStatusEnum.getMsgByStatus(borrower.getStatus());
        borrowerDetailVO.setStatus(status);

        List<BorrowerAttachVO> list = iBorrowerAttachService.selectBorrowerAttachVOList(id);



        return borrowerDetailVO;
    }

    /**
     * 额度审批
     * @param borrowerApprovalVO
     */
    @Override
    @Transactional
    public void approavl(BorrowerApprovalVO borrowerApprovalVO) {

        //借款人认证状态
        //修改借款人状态信息
        Long borrowerId = borrowerApprovalVO.getBorrowerId();
        Borrower borrower = baseMapper.selectById(borrowerId);
        borrower.setStatus(borrowerApprovalVO.getStatus());
        baseMapper.updateById(borrower);

        //查询出用户信息并给用户添加积分
        Long userId = borrower.getUserId();
        UserInfo userInfo = userInfoMapper.selectById(userId);

        //添加用户积分记录
        UserIntegral userIntegral = new UserIntegral();
        userIntegral.setUserId(userId);
        userIntegral.setIntegral(borrowerApprovalVO.getInfoIntegral());
        userIntegral.setContent("借款人基本信息");
        userIntegralMapper.insert(userIntegral);


        int curIntegral = userInfo.getIntegral() + borrowerApprovalVO.getInfoIntegral();
        if(borrowerApprovalVO.getIsIdCardOk()) {
            curIntegral += IntegralEnum.BORROWER_IDCARD.getIntegral();
            userIntegral = new UserIntegral();
            userIntegral.setIntegral(IntegralEnum.BORROWER_IDCARD.getIntegral());
            userIntegral.setUserId(userId);
            userIntegral.setContent(IntegralEnum.BORROWER_IDCARD.getMsg());
            userIntegralMapper.insert(userIntegral);
        }

        if (borrowerApprovalVO.getIsCarOk()){
            curIntegral += IntegralEnum.BORROWER_CAR.getIntegral();
            userIntegral = new UserIntegral();
            userIntegral.setIntegral(IntegralEnum.BORROWER_CAR.getIntegral());
            userIntegral.setUserId(userId);
            userIntegral.setContent(IntegralEnum.BORROWER_CAR.getMsg());
            userIntegralMapper.insert(userIntegral);
        }
        if (borrowerApprovalVO.getIsHouseOk()){
            curIntegral += IntegralEnum.BORROWER_HOUSE.getIntegral();
            userIntegral = new UserIntegral();
            userIntegral.setIntegral(IntegralEnum.BORROWER_HOUSE.getIntegral());
            userIntegral.setUserId(userId);
            userIntegral.setContent(IntegralEnum.BORROWER_HOUSE.getMsg());
            userIntegralMapper.insert(userIntegral);
        }
        //修改审核状态
        userInfo.setIntegral(curIntegral);
        userInfo.setBindStatus(borrowerApprovalVO.getStatus());
        userInfoMapper.updateById(userInfo);


    }
}
